package com.example.smart.ui.activity.home

import android.view.MotionEvent
import android.view.View
import com.example.lib.extend.dip2px
import com.example.lib.extend.horizontalManager
import com.example.lib.extend.setOnDoubleClickListener
import com.example.lib.utils.ToastUtils
import com.example.lib.utils.statusbar.StatusBarUtils
import com.example.smart.R
import com.example.smart.adapter.home.QuestionAdapter
import com.example.smart.base.ui.activity.BaseActivity
import com.example.smart.data.QuestionData
import com.example.smart.databinding.ActivityQuestionBinding
import com.example.smart.dialog.QuestionDoneDialog
import com.example.smart.utils.DisplayUtils


class QuestionActivity : BaseActivity<ActivityQuestionBinding>() {
    private lateinit var mAdapter: QuestionAdapter
    private var itemWidth = 0
    private val mDialog by lazy {
        QuestionDoneDialog(this@QuestionActivity)
    }

    override fun layoutID() = R.layout.activity_question

    override fun initView() {
        StatusBarUtils.setStateAndNavigationBar(this@QuestionActivity,
            statusBarColor = com.example.lib.R.color.black_ececf8)

        itemWidth = dip2px(276f)

        val data = mutableListOf<QuestionData>()
        data.add(QuestionData("", "", "", "", "", "", "", default = true))

        data.add(QuestionData("疫苗接种前是否需要检查牲畜健康状况？",
            "A. 必须",
            "B. 不必",
            "C. 随意",
            "D. 仅幼畜",
            "A",
            ""))
        data.add(QuestionData("疫苗保存温度错误的是？", "A. -20℃", "B. 4℃", "C. 25℃", "D. 2-8℃", "C", ""))
        data.add(QuestionData("疫苗接种后建议观察牲畜多久？", "A. 12h", "B. 24h", "C. 48h", "D. 72h", "C", ""))

        data.add(QuestionData("废弃疫苗瓶正确处理方式？", "A. 焚烧", "B. 填埋", "C. 回收", "D. 开水烫", "A", ""))
        data.add(QuestionData("不同疫苗最短接种间隔应超过？", "A. 3天", "B. 7天", "C. 10天", "D. 14天", "D", ""))

        data.add(QuestionData("", "", "", "", "", "", "", default = true))
        binding.Indicator.addIndicator(data.size - 2)
        mAdapter = QuestionAdapter(this@QuestionActivity)

        binding.Recycle.horizontalManager()
        binding.Recycle.adapter = mAdapter
        mAdapter.updateList(data)
        binding.Recycle.smoothScrollBy(dip2px(250f), 0)
    }

    override fun initListener() {
        binding.Go.setOnDoubleClickListener {
            if (binding.Go.text == "完成") {
                mDialog.show()
            } else {
                if (mAdapter.getItemAtPosition(mAdapter.choose).select.isEmpty()) {
                    ToastUtils.showToast("请选择！")
                    return@setOnDoubleClickListener
                }
                binding.Recycle.smoothScrollBy(itemWidth + dip2px(15f), 0)
                mAdapter.next()
                binding.Indicator.setSelectIndicator(mAdapter.choose - 1)

                if (mAdapter.size() - 2 == mAdapter.choose) {
                    binding.Go.text = "完成"
                }
            }
        }

        binding.Exit.setOnDoubleClickListener {
            finish()
        }

        mDialog.onClick = {
            mDialog.dismiss()
            DisplayUtils.toRank(this@QuestionActivity)
            finish()
        }

        binding.Recycle.setOnTouchListener(object : View.OnTouchListener {
            override fun onTouch(p0: View, p1: MotionEvent): Boolean {
                if (p1.action == MotionEvent.ACTION_MOVE) {
                    return true
                } else return false
            }
        })
    }
}